package Utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 创建JDBCUtils 工具类
 */
public class JDBCUtils {
    private static String url=null;
    private static String username=null;
    private static String password=null;
    private static String driver=null;
    static {
        Properties pro=new Properties();
        InputStream jdbcUtils = JDBCUtils.class.getClassLoader().getResourceAsStream("JDBCUtils.properties");
        try {
            pro.load(jdbcUtils);
           url = pro.getProperty("url");
           username = pro.getProperty("username");
           password=pro.getProperty("password");
           driver = pro.getProperty("driver");
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {

        return DriverManager.getConnection(url,username,password);
    }
    public static void close(PreparedStatement sate, Connection conn){
        if (sate!=null){
            try {
                sate.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public static void close(ResultSet res, PreparedStatement sate, Connection conn){
        if (res!=null){
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (sate!=null){
            try {
                sate.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
